package cn.jsj.cxsys.service.impl;

import org.springframework.stereotype.*;
import javax.annotation.*;
import cn.jsj.cxsys.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.jsj.cxsys.dao.*;
import org.springframework.transaction.annotation.*;
import java.util.*;
import com.github.pagehelper.*;
import cn.jsj.cxsys.pojo.*;

@Service
public class StudentLabServiceImpl implements StudentLabService
{
    @Resource
    private StudentDoLab studentDoLab;
    @Resource
    private LabFunction labFunction;
    @Autowired
    private UserService userService;
    @Resource
    private UserFunction userFunction;
    @Resource
    private RecordFunction recordFunction;

    @Transactional
    @Override
    public String ApplyLab(final LabMessage labMessage, final Student student) {
        final String inf = this.userService.login(student);
        if (!inf.equals("student")) {
            return "error";
        }
        if (labMessage == null || labMessage.getMessageInf() == null || labMessage.getMessageInf().equals("") || labMessage.getMessageInf().trim().length() < 1 || labMessage.getStudentID() < 1 || labMessage.getLabID() < 1) {
            return "paramLess";
        }
        if (labMessage.getStudentID() != student.getUserID()) {
            return "error";
        }
        final int k = this.studentDoLab.checkStudentHavePrivelageOfLab(labMessage.getLabID(), student.getUserID());
        if (k != 0) {
            return "Appled";
        }
        labMessage.setApplyTime(NowDateFactory.getNowTime());
        final int j = this.studentDoLab.checkPushedThisMessage(labMessage);
        if (j == 1) {
            return "ApplyExist";
        }
        final String time = NowDateFactory.getNowTime();
        labMessage.setApplyTime(time);
        final int i = this.studentDoLab.pushApplyMessage(labMessage);
        if (i == 1) {
            return "success";
        }
        return "error";
    }

    @Override
    public List<LabMessage> getLabMessages(final User student, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(student);
        if (!inf.equals("student")) {
            return null;
        }
        if (student.getUserID() < 1) {
            return null;
        }
        PageHelper.startPage(pageNum, pageSize);
        return this.studentDoLab.getApplyResult(student.getUserID());
    }

    @Override
    public List<Lab> getLabList() {
        return this.labFunction.getLabs();
    }

    @Override
    public List<Lab> getLabOfStudentAppled(final Student student) {
        if (student == null || student.getUserID() < 1) {
            return null;
        }
        return this.studentDoLab.getAppledLabs(student.getUserID());
    }

    @Override
    public List<Integer> getStudentAppledLabID(final User user) {
        if (user == null || user.getUserID() < 1) {
            return null;
        }
        final String inf = this.userService.login(user);
        if (inf.equals("error") || "superLader".equals(inf)) {
            return null;
        }
        return this.studentDoLab.getAppledLabIDs(user.getUserID());
    }

    @Override
    public List<LabMessage> getAllMessageStudent(final Student student) {
        final String inf = this.userService.login(student);
        if (inf.equals("student")) {
            return this.studentDoLab.getAllMessageOfStudent(student.getUserID());
        }
        return null;
    }

    @Transactional
    @Override
    public String studentSigInLab(final User user, final int labID) {
        if (labID < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!inf.equals("student")) {
            return "error";
        }
        final Lab lab = this.labFunction.getLabByID(labID);
        if (lab == null) {
            return "error";
        }
        final String sta = this.userService.checkStudentState(user);
        if (sta.equals("error") || sta.equals("inLab") || sta.equals("onComputer")) {
            return "error";
        }
        final int k = this.studentDoLab.checkStudentHavePrivelageOfLab(lab.getLabID(), user.getUserID());
        if (k == 0) {
            return "YouNeetToApply";
        }
        final String time = NowDateFactory.getNowTime();
        int i = this.recordFunction.siginLab(user.getUserID(), lab.getLabID(), time);
        if (i != 1) {
            return "error";
        }
        i = this.labFunction.labPeopleAddOne(lab.getLabID());
        if (i == 1) {
            this.userFunction.userAddOnline(user.getUserID());
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String studentSignOutLab(final User user, final String str) {
        if (str == null || str.trim().length() == 0) {
            return "paramLess";
        }
        String inf = this.userService.login(user);
        if (!inf.equals("student")) {
            return inf;
        }
        inf = this.userService.checkStudentState(user);
        if (!inf.equals("inLab")) {
            return inf;
        }
        final int labID = this.recordFunction.getLabIdOfUserIn(user.getUserID());
        final String datestr = NowDateFactory.getNowTime();
        int i = this.recordFunction.sigoutRecord(user.getUserID(), datestr, str);
        if (i != 1) {
            return "error";
        }
        i = this.labFunction.labPeopleDecline(labID);
        if (i == 1) {
            this.userFunction.deleteOneOnlineRecord(user.getUserID());
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String deleteOneMessage(final User user, final LabMessage labMessage) {
        if (labMessage == null || labMessage.getMessageID() < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!inf.equals("student")) {
            return inf;
        }
        final int i = this.studentDoLab.deleteOneMessage(user.getUserID(), labMessage.getMessageID());
        if (i == 1) {
            return "success";
        }
        return "error";
    }
}
